<template>
    <div class="debris-list" v-loading="loading">
        <div :class="{ screen: userId }">
            <div v-if="userId && fromType === '1'" class="input-box" :class="{ 'rent-input-box': userId }">
                <span class="input-title" style="margin-left:0px;">游戏</span>
                <el-select @change="gameChange" style="width:150px;" v-model="gameChangeVal">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="王者荣耀" value="1104466820"></el-option>
                    <el-option label="和平精英" value="1106467070"></el-option>
                </el-select>
            </div>
            <div class="input-box" :class="{ 'rent-input-box': userId }">
                <template v-if="fromType !== '1'">
                    <span class="input-title" style="margin-left:0px;">条件</span>
                    <el-input
                        placeholder="搜索趣ID、订单号、QQ号、游戏昵称"
                        @keyup.enter.native="selectChange"
                        style="width:300px;"
                        clearable
                        v-model.trim="keyWord">
                        <i slot="prefix" class="el-input__icon el-icon-search"></i>
                    </el-input>
                </template>
                <span class="input-title">下单时间</span>
                <el-date-picker
                    @change="getList(1)"
                    v-model="date"
                    :clearable="true"
                    type="daterange"
                    value-format="yyyy-MM-dd"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker>
                <el-button type="primary" @click="resetBtn" style="margin-left: 20px">重置</el-button>
            </div>
            <div class="input-box" :class="{ 'rent-input-box': userId }" v-if="fromType !== '1'">
                <span class="input-title" style="margin-left:0px;">状态</span>
                <el-select @change="getList(1)" style="width:150px;" v-model="status">
                    <el-option label="全部" :value="null"></el-option>
                    <el-option label="体验中" :value="0"></el-option>
                    <el-option label="待结算" :value="1"></el-option>
                    <el-option label="已完成" :value="2"></el-option>
                    <el-option label="待罚款" :value="3"></el-option>
                </el-select>
                <el-button type="primary" class="btn" style="margin-left:20px" @click="getList(1)">查询</el-button>
            </div>
        </div>
        <div class="table-box">
            <el-table :data="tableData" ref="table" border style="width: 100%" @sort-change="sortChange">
                <el-table-column fixed width="160" prop="orderNo" label="订单号">
                    <template slot-scope="scope">
                        <p class="table-btn" @click="goOrderDetail(scope.row)">{{ scope.row.orderNo }}</p>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="游戏">
                    <template slot-scope="scope">
                        <span>{{ scope.row.gameName }}</span>
                    </template>
                </el-table-column>
                <el-table-column min-width="150" label="账号">
                    <template slot-scope="scope">
                        <p class="table-btn" @click="goQQDetails(scope.row)">{{ scope.row.accountVo.accountNo }}</p>
                        <p>{{ scope.row.accountVo.serverName }}</p>
                    </template>
                </el-table-column>
                <el-table-column min-width="150" label="号主">
                    <template slot-scope="scope">
                        <p class="table-btn" @click="goUserDetail(scope.row.accountVo.userId )">{{ scope.row.accountVo.userId }}</p>
                    </template>
                </el-table-column>
                <el-table-column min-width="150" prop="perHourPrice" label="租用价格" sortable="custom">
                    <template slot-scope="scope">
                        <p> {{ scope.row.perHourPrice }}</p>
                    </template>
                </el-table-column>
                
                <el-table-column min-width="120" label="体验时长" prop="gameTime" sortable="custom"></el-table-column>
                <el-table-column min-width="120" label="订单金额">
                    <template slot-scope="scope">
                        <p> {{ scope.row.orderPrice }}</p>
                    </template>
                </el-table-column>
                <el-table-column min-width="120" label="号主收益">
                    <template slot-scope="scope">
                        <p> {{ scope.row.incomePrice }}</p>
                    </template>
                </el-table-column>
                <el-table-column min-width="150" label="状态">
                    <template slot-scope="scope">
                        <span>{{ scope.row.orderStatus }}</span>
                    </template>
                </el-table-column>
                
                <el-table-column width="170" label="下单时间">
                    <template slot-scope="scope">
                        <div>{{ scope.row.createTime }}</div>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tools">
            <el-pagination
                @size-change="sizeChange"
                @current-change="pageChange"
                :current-page="pageNumber"
                :page-sizes="_pageSizes"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
import { formatDate } from '@/utils/utils'

export default {
    props: ['userId', 'fromType'],
    data () {
        return {
            pageNumber: 1,
            pageSize: 10,
            keyWord: '',
            status: null,
            date: [],
            tableData: [],
            dialogParams: {
            },
            total: 0,
            isAddPay: false,
            loading: false,
            dialogVisible: false,
            type: '',
            sort: '',
            gameId: '1104466820',
            gameChangeVal: ''
        }
    },
    watch: {
        $route: function (route) {
            this.gameId = route.query.gameId ? route.query.gameId : this.gameId
            this.keyWord = ''
            this.getList(1)
        },
        immediate: true
    },
    created () {
        this.getList(1)
    },
    methods: {
        resetBtn () {
            this.date = []
            this.gameChange('')
        },
        // 后端排序
        sortChange ({ column, prop, order }) {
            this.sort = order || ''
            // 如果没有 sort prop 置为空
            if (this.sort === 'ascending') {
                this.sort = 'asc'
            } else {
                this.sort = 'desc'
            }
            this.type = prop === 'perHourPrice' ? '0' : '1'
            this.getList(1)
        },
        // 游戏类型切换
        gameChange (val) {
            this.gameChangeVal = val
            this.getList(1)
        },
        // 获取列表
        async getList (pageNumber) {
            if (this.loading) return
            this.pageNumber = pageNumber
            this.loading = true
            let params = {
                pageNumber,
                pageSize: this.pageSize,
                fromDate: '',
                endDate: '',
                listType: 0,
                gameId: this.$route.query.gameId ? this.$route.query.gameId : this.gameChangeVal
            }

            params.orderByType = this.type
            params.sortType = this.sort
            // if (this.keyWord) {
            //     params.keyWord = this.keyWord
            // }
            // if (this.userId) params.keyWord = this.userId
            // params.status = this.status
            params.userId = this.userId

            if (this.date && this.date.length) {
                params.fromDate = this.date[0] + ' 00:00:00'
                params.endDate = this.date[1] + ' 23:59:59'
            }

            let res = await this.$http.get('/trade/userRentOrderList', {
                params
            })
            if (res.code === '0') {
                this.tableData = res.data.tradeOrderVos
                this.total = res.count
                let tip_res = {
                    lendAccountNum: res.data.lendAccountNum,
                    lendPrice: res.data.lendPrice,
                    rentAccountNum: res.data.rentAccountNum,
                    rentPrice: res.data.rentPrice,
                }
                this.$emit('getRentNum', res.data.rentAccountNum, res.data)
            } else {
                this.$message({
                    type: 'error',
                    message: '系统异常，请稍后再试'
                })
            }
            this.loading = false
        },
        addPay (item) {
            this.dialogVisible = true
            this.isAddPay = true
            this.dialogTitle = '请输入追加金额与理由'
            this.dialogParams.orderId = item.id
        },
        // 点击QQ账号 跳转到相应的 租号账号详情
        goQQDetails (item) {
            let path = '/wzry/accDetail'
            if (item.gameName === '和平精英') {
                path = '/hpjy/accManageDetail'
            }
            const { href } = this.$router.resolve({
                path,
                query: {
                    accountId: item.accountVo.accountNo, userId: item.accountVo.userId, dataId: item.dataId
                }
            })
            window.open(href, '_blank')
        },
        goOrderDetail (item) {
            let { orderId, gameName} = item
            let path = '/wzry/orderManageDetail'
            if (gameName === '和平精英') {
                path = '/hpjy/orderManageDetail'
            }
            const { href } = this.$router.resolve({
                path,
                query: {
                    id: orderId
                }
            })
            window.open(href, '_blank')
        },
        // 查看用户详情
        goUserDetail (id) {
            const { href } = this.$router.resolve({
                path: '/dataReport/userInfo',
                query: {
                    id
                }
            })
            window.open(href, '_blank')
        },
        selectChange () {
            this.type = ''
            this.sort = ''
            this.$refs.table.clearSort()
            this.getList(1)
        },
        orderback (item) { // 退款
            this.dialogVisible = true
            this.isAddPay = false
            this.dialogTitle = '请输入退款金额'
            this.dialogParams.orderId = item.id
        },
        dialogSure () {
            if (this.dialogParams.piece < 0) {
                return this.$message({
                    type: 'error',
                    message: '请输入正确的价格'
                })
            }
            let url = '/trade/orderDrawBack'
            if (this.isAddPay) {
                url = '/trade/addPunish'
            }
            this.$http.post(url, this.dialogParams).then(res => {
                if (res.code === '0') {
                    this.dialogVisible = false
                    this.dialogParams = {}
                    this.getList(1)
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    })
                } else {
                    this.$message({
                        type: 'error',
                        message: res.desc
                    })
                }
            })
        },
        // 页码切换
        sizeChange (val) {
            this.pageNumber = 1
            this.pageSize = val
            this.getList(1)
        },

        // 页容切换
        pageChange (val) {
            this.pageNumber = val
            this.getList(this.pageNumber)
        },
        tableFormatDate (time) {
            return formatDate(time, 'YYYY-MM-DD HH:MM:SS')
        }
    }
}
</script>

<style lang="scss" scoped>
    .debris-list {
        .screen{
            display: flex;
            align-items: center;
            padding: 20px 0;
        }
        .input-box {
            margin-top: 10px;
            font-size: 13px;
            font-weight: 500;
            .input-title {
                margin: 0 10px 0 20px;
            }
            .btn {
                margin-top: 10px;
                margin-left: 10px;
            }
        }
        .rent-input-box{
            margin: 0 10px 0 0;
            .btn{
                margin-top: 0;
            }
        }

        .table-box {
            margin: 20px 0;
            .cover {
                width: 80px;
                height: 80px;
                > img {
                    width: 100%;
                    height: 100%;
                }
            }
            .desc {
                overflow: hidden;
                line-height: 20px;
                -webkit-line-clamp: 5;
                -webkit-box-orient: vertical;
                display: -webkit-box;
                height: 100px;
            }
            .user-icon {
                width: 20px;
                height: 20px;
                border-radius: 50%;
                margin-right: 5px;
                vertical-align: top;
            }
            .table-btn {
                cursor: pointer;
                color: #409EFF;
                font-size: 13px;
                font-weight: 500;
                &:hover {
                    text-decoration: underline;
                }
            }
            .point {
                display:inline-block;
                width:10px;
                height:10px;
                border-radius: 50%;
                background-color:#67C23A;
                margin-right:3px;
            }
            .red-point {
                background-color: #F56C6C;
            }
        }

        .tools {
            padding-right: 20px;
            display: flex;
            justify-content: flex-end;
        }
        .dialog-reason {
            margin-top: 20px;
            p {
                margin-bottom: 10px;
            }
        }
    }
</style>
